package com.mdd.front.service.product.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdd.front.service.product.IProductTypeFrontService;
import com.mdd.front.vo.property.ProductTypeVo;
import com.mdd.product.entity.ProductType;
import com.mdd.product.mapper.ProductTypeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 商品类别实现类
 * @author LikeAdmin
 */
@Service
public class ProductTypeServiceFrontImpl implements IProductTypeFrontService {

    @Resource
    ProductTypeMapper productTypeMapper;

    /**
     * 商品类别列表
     *
     * @author LikeAdmin
     * @return PageResult<ProductTypeListedVo>
     */
    @Override
    public List<ProductTypeVo> list() {
        LambdaQueryWrapper<ProductType> productType=new LambdaQueryWrapper<>();
        List<ProductType> data = productTypeMapper.selectList(productType);
        return recursionNextRoute(0L,data);
    }
    private List<ProductTypeVo> recursionNextRoute(Long parentId, List<ProductType> menuList) {
        List<ProductTypeVo> list = new ArrayList<>();
        Optional.ofNullable(menuList).ifPresent(menus -> menus.stream()
                .filter(menu -> parentId.equals(menu.getPId()))
                .forEach(menu -> {
                    ProductTypeVo vo = new ProductTypeVo();
                    BeanUtils.copyProperties(menu, vo);
                    // 递归获取子节点
                    List<ProductTypeVo> children = recursionNextRoute(menu.getId(), menuList);
                    vo.setChildren(children);
                    list.add(vo);
                }));
        return list;
    }
}
